import router from '@/router/index.js'
import api from '@/api/index.js'
import {userStore} from '@/stores/user.js'

export const getToken = () => {
    let tokenData = localStorage.getItem('tokenData')
    if (tokenData) {
        tokenData = JSON.parse(tokenData)
        return tokenData?.access_token
    }
    return ''
}

export const logout = () => {
    if (getToken()) {
        api.logout().finally(() => {
            closeData()
        })
    } else {
        closeData()
    }
    console.log('logout******')

    router.push({
        path: '/login'
    })
}

export const closeData = () => {
    const user = userStore()
    user.setUserInfo(null)
    localStorage.removeItem('tokenData')
    sessionStorage.removeItem('userInfo')
}